<?php

class FacebookPropio {

    public static function getUser($id = null) {
        $graph_url = "https://graph.facebook.com/" . $id;
        $user = json_decode(file_get_contents($graph_url));
        if ($user) {
            return $user;
        } else {
            return 'Usuario ya no existe.';
        }
    }

    public static function check_connection() {

        //if(session_id()==""){
        session_name($_SERVER["REMOTE_ADDR"] . "-preuvideo");
        session_start();
        //}
        if (isset($_SESSION['state']) && isset($_SESSION['access_token'])) {
            if ($_SESSION['state'] && $_SESSION['access_token']) {
                $graph_url = "https://graph.facebook.com/me?access_token="
                        . $_SESSION['access_token'];
                $user = json_decode(file_get_contents($graph_url));
                if ($user) {
                    return $user;
                } else {
                    unset($_SESSION['state']);
                    unset($_SESSION['access_token']);
                    return false;
                }
            }
        }

        return false;
    }

    public static function sacarIdMaximo($tabla) {
        $connection = Doctrine_Manager::connection();
        $query = 'SELECT MAX(id) AS max FROM ' . $tabla;
        $statement = $connection->execute($query);
        $statement->execute();
        $resultset = $statement->fetch(PDO::FETCH_OBJ);
        $max = $resultset->max;
        return $max;
    }

    public static function facebookQuery($ids) {

        $app_id = sfConfig::get('app_facebook_id');
        $app_secret = sfConfig::get('app_facebook_secret');

        $config = array(
            'appId' => $app_id,
            'secret' => $app_secret,
            'cookie' => false,
        );

        $arrayIds = '(';
        foreach ($ids as $i => $id) {
            if ($i != sizeof($ids)-1) {
                $arrayIds = $arrayIds . $id . ',';
            }
            else{
                $arrayIds = $arrayIds . $id . ')';
            }
        }

        $facebook = new Facebook($config);

        $params = array(
            'method' => 'fql.query',
            'query' => "SELECT uid,name,pic_small FROM user WHERE uid IN " . $arrayIds,
        );

        $result = $facebook->api($params);


        return $result;
    }

}

